package com.hddf.project.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.hddf.project.base.BaseDaoImpl;
import com.hddf.project.dao.SecondCategoryDao;
import com.hddf.project.pojos.SecondCategory;

public class SecondCategoryDaoImpl extends BaseDaoImpl implements SecondCategoryDao {

	/**
	 * 添加二级类别
	 * 
	 * @param sc
	 * @param s
	 */
	@Override
	public void add(SecondCategory sc, Session s) throws Exception {
		super.add(sc, s);
	}

	/**
	 * 删除二级类别
	 * 
	 * @param second_category_id
	 * @param s
	 */
	@Override
	public void delete(int second_category_id, Session s) throws Exception {
		super.deleteById(SecondCategory.class, second_category_id, s);
	}

	/**
	 * 删除二级类别
	 * 
	 * @param sc
	 * @param s
	 */
	@Override
	public void delete(SecondCategory sc, Session s) throws Exception {
		super.delete(sc, s);
	}

	/**
	 * 获取指定二级类别
	 * 
	 * @param second_category_id
	 * @param s
	 * @return
	 */
	@Override
	public SecondCategory get(int second_category_id, Session s) throws Exception {
		return (SecondCategory) super.get(SecondCategory.class, second_category_id, s);
	}

	/**
	 * 修改二级类别
	 * 
	 * @param sc
	 * @param s
	 */
	@Override
	public void update(SecondCategory sc, Session s) throws Exception {
		super.update(sc, s);
	}

	/**
	 * 根据名称获取二级类别
	 * 
	 * @param name
	 * @param s
	 * @return
	 */
	@Override
	public SecondCategory getByName(String name, Session s) throws Exception {
		Query query = s.createQuery("from SecondCategory sc where sc.second_category_name=:temp ");
		query.setString("temp", name);
		SecondCategory sc = (SecondCategory) query.uniqueResult();
		return sc;
	}

	/**
	 * 获取全部二级类别
	 * 
	 * @param s
	 * @return
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<SecondCategory> getList(Session s) throws Exception {
		List<SecondCategory> list = super.find("from SecondCategory sc order by sc.sort asc", s);
		return list;
	}

	/**
	 * 根据一级类别ID获取二级类别
	 * 
	 * @param first_category_id
	 * @param s
	 * @return
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<SecondCategory> getListByFirstCategory(int first_category_id, Session s) {
		Query query = s
				.createQuery("from SecondCategory as sc  where sc.first_category_id=:fid order by sc.sort asc");
		query.setInteger("fid", first_category_id);
		return query.list();
	}

	/**
	 * 根据一级类别ID获取小于等于排序数的二级子类别
	 * 
	 * @param first_category_id
	 * @param s
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<SecondCategory> listSecondCategory(int first_category_id, int sort, Session s) {
		Query query = s
				.createQuery("from SecondCategory as sc where sc.first_category_id=:fid order by sc.sort asc");
		query.setInteger("fid", first_category_id);
		if (0 != sort) {// 分页
			query.setFirstResult(0);
			query.setMaxResults(sort);
		}
		return query.list();
	}
}
